#include "Frm09.h"

Frm09::Frm09(HmiApp* app, STTWidget *par, const string& id, const string& desc) : STTPage(app->getRuntime(), par, id, desc) {
	hmiApp = app;
}

Frm09::~Frm09() {
}

void Frm09::onInit() {

}

void Frm09::createParts() {
	setMainArg(0, 0, 1024, 600);
	setMainStyle(0, 0xffffff, 0xffffff, 0, 255, 0, 0xc0c0c0);

	/*----------------------------- Panel : wMPanel1                     -----------------------------*/
	wMPanel1 = new STTPanel(p_rt, this, "wMPanel1");
	wMPanel1->setMainArg(283, 275, 667, 316, 0);
	wMPanel1->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 255, 1, 0xc0c0c0);

	/*----------------------------- Chart : wMChart1                     -----------------------------*/
	wMChart1 = new STTChart(p_rt, wMPanel1, "wMChart1");
	wMChart1->setMainArg(20, 20, 625, 279, 1, 10, 4, 8, 12, "文泉驿微米黑", 0x0, 0);
	wMChart1->setMainStyle(0, 0xffffff, 0xffffff, 0, 0, 0xc0c0c0, 0, 0x0, 50, 30, 20, 50);
	wMChart1->setXAxis(0, 0, 10, 1, true, "0\n1\n2\n3\n4\n5\n6\n7\n8\n9");
	wMChart1->setYAxis(0, 0, 6, 1, true, "100\n80\n60\n40\n20\n0", 0.0, 100.0);
	wMChart1->setY2Axis(0, 0, 2, 1, true, "\n", 0, 100);
	wMChart1->setChartBgStyle(0, 0xf0f0f0, 0xffffff, 0, 0, 0xc0c0c0, 2, 4, 1, 0xc0c0c0, 5, 0);
	wMChart1->setTickStyle(1, 0xc0c0c0);
	wMChart1->hideSeries(wMChart1->setAddSeries(0, 0xff0000, NULL, 0, 100), false);

	/*----------------------------- MButton : wMMButton1                 -----------------------------*/
	wMMButton1 = new STTButton(p_rt, this, "wMMButton1", 1);
	wMMButton1->setMainArg(0, 0, 100, 50, false);
	wMMButton1->setImg("1", "./images/arrow_34_100X50.gray.svg", "");
	wMMButton1->setImg("0", "./images/arrow_34_100X50.svg", "");
	wMMButton1->bindOpenFormHandler(Event::CLICKED, "Frm01", PageMode::NEWWIN, NULL);

	/*----------------------------- Panel : wMPanel2                     -----------------------------*/
	wMPanel2 = new STTPanel(p_rt, this, "wMPanel2");
	wMPanel2->setMainArg(297, 0, 121, 201, 0);
	wMPanel2->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 255, 1, 0xc0c0c0);

	/*----------------------------- Slider : wMSlider2                   -----------------------------*/
	wMSlider2 = new STTSlider(p_rt, wMPanel2, "wMSlider2");
	wMSlider2->setMainArg(21, 13, 9, 167, 50, 0, 255, false);
	wMSlider2->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider2->setIndicStyle(9999, 0xff0000, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider2->setKnobStyle(7, 0xff0000, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider2->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm09::wMSlider2_vch_cb);

	/*----------------------------- Slider : wMSlider3                   -----------------------------*/
	wMSlider3 = new STTSlider(p_rt, wMPanel2, "wMSlider3");
	wMSlider3->setMainArg(55, 13, 9, 167, 50, 0, 255, false);
	wMSlider3->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider3->setIndicStyle(9999, 0xff00, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider3->setKnobStyle(7, 0xff00, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider3->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm09::wMSlider3_vch_cb);

	/*----------------------------- Slider : wMSlider4                   -----------------------------*/
	wMSlider4 = new STTSlider(p_rt, wMPanel2, "wMSlider4");
	wMSlider4->setMainArg(89, 13, 9, 167, 50, 0, 255, false);
	wMSlider4->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider4->setIndicStyle(9999, 0xff, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider4->setKnobStyle(7, 0xff, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider4->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm09::wMSlider4_vch_cb);

	/*----------------------------- Slider : wMSlider9                   -----------------------------*/
	wMSlider9 = new STTSlider(p_rt, this, "wMSlider9");
	wMSlider9->setMainArg(264, 17, 9, 167, 50, 0, 255, false);
	wMSlider9->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider9->setIndicStyle(9999, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider9->setKnobStyle(7, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider9->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm09::wMSlider9_vch_cb);

	/*----------------------------- Label : wMLabel3                     -----------------------------*/
	wMLabel3 = new STTLabel(p_rt, this, "wMLabel3");
	wMLabel3->setMainArg(232, 220, 74, 32, "不透明度(0-255)", TextAlign::CENTER, LabelLongMode::WRAP, 30);
	wMLabel3->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel3->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel3->setFormatText(TextAlign::CENTER, LabelLongMode::WRAP, 30);

	/*----------------------------- Label : wMLabel4                     -----------------------------*/
	wMLabel4 = new STTLabel(p_rt, this, "wMLabel4");
	wMLabel4->setMainArg(321, 220, 74, 16, "主色", TextAlign::CENTER, LabelLongMode::WRAP, 30);
	wMLabel4->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel4->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel4->setFormatText(TextAlign::CENTER, LabelLongMode::WRAP, 30);

	/*----------------------------- Slider : wMSlider10                  -----------------------------*/
	wMSlider10 = new STTSlider(p_rt, this, "wMSlider10");
	wMSlider10->setMainArg(435, 17, 9, 167, 0, 0, 100, false);
	wMSlider10->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider10->setIndicStyle(9999, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider10->setKnobStyle(7, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider10->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm09::wMSlider10_vch_cb);

	/*----------------------------- Label : wMLabel6                     -----------------------------*/
	wMLabel6 = new STTLabel(p_rt, this, "wMLabel6");
	wMLabel6->setMainArg(403, 220, 74, 32, "宽度", TextAlign::CENTER, LabelLongMode::WRAP, 30);
	wMLabel6->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel6->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel6->setFormatText(TextAlign::CENTER, LabelLongMode::WRAP, 30);

	/*----------------------------- Slider : wMSlider11                  -----------------------------*/
	wMSlider11 = new STTSlider(p_rt, this, "wMSlider11");
	wMSlider11->setMainArg(635, 17, 9, 167, 0, 0, 100, false);
	wMSlider11->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider11->setIndicStyle(9999, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider11->setKnobStyle(7, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider11->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm09::wMSlider11_vch_cb);

	/*----------------------------- Slider : wMSlider12                  -----------------------------*/
	wMSlider12 = new STTSlider(p_rt, this, "wMSlider12");
	wMSlider12->setMainArg(708, 17, 9, 167, 0, 0, 100, false);
	wMSlider12->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider12->setIndicStyle(9999, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider12->setKnobStyle(7, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider12->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm09::wMSlider12_vch_cb);

	/*----------------------------- Label : wMLabel7                     -----------------------------*/
	wMLabel7 = new STTLabel(p_rt, this, "wMLabel7");
	wMLabel7->setMainArg(603, 220, 74, 32, "虚线间隙", TextAlign::CENTER, LabelLongMode::WRAP, 30);
	wMLabel7->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel7->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel7->setFormatText(TextAlign::CENTER, LabelLongMode::WRAP, 30);

	/*----------------------------- Label : wMLabel8                     -----------------------------*/
	wMLabel8 = new STTLabel(p_rt, this, "wMLabel8");
	wMLabel8->setMainArg(676, 220, 74, 32, "虚线实长", TextAlign::CENTER, LabelLongMode::WRAP, 30);
	wMLabel8->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel8->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel8->setFormatText(TextAlign::CENTER, LabelLongMode::WRAP, 30);

	/*----------------------------- Slider : wMSlider13                  -----------------------------*/
	wMSlider13 = new STTSlider(p_rt, this, "wMSlider13");
	wMSlider13->setMainArg(807, 17, 9, 167, 0, 0, 1, false);
	wMSlider13->setMainStyle(9999, 0xa0a0a0, 0xffffff, 0, 0, 0xa0a0a0, 0, 0x0, NULL);
	wMSlider13->setIndicStyle(9999, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0xffffff, NULL);
	wMSlider13->setKnobStyle(7, 0x0, 0xffffff, 0, 0, 0x1a2b1, 0, 0x0, NULL, 7);
	wMSlider13->onEventHandler(Event::VALUE_CHANGED, this, (EHandler) &Frm09::wMSlider13_vch_cb);

	/*----------------------------- Label : wMLabel9                     -----------------------------*/
	wMLabel9 = new STTLabel(p_rt, this, "wMLabel9");
	wMLabel9->setMainArg(775, 220, 74, 16, "圆帽", TextAlign::CENTER, LabelLongMode::WRAP, 30);
	wMLabel9->setFonts(0x0, 12, "文泉驿微米黑", 0);
	wMLabel9->setMainStyle(0, 0xffffff, 0xffffff, GradDir::NONE, 0, 0x0, 0);
	wMLabel9->setFormatText(TextAlign::CENTER, LabelLongMode::WRAP, 30);

	/*----------------------------- Label : wMLabel10                    -----------------------------*/
	wMLabel10 = new STTLabel(p_rt, this, "wMLabel10");
	wMLabel10->setMainArg(0, 49, 227, 508, "直线演示", TextAlign::CENTER, LabelLongMode::CLIP, 30);
	wMLabel10->setFonts(0xff0000, 28, "文泉驿微米黑", 0);
	wMLabel10->setMainStyle(0, 0x80ffff, 0xffffff, GradDir::NONE, 0, 0x0, 140);
	wMLabel10->setFormatText(TextAlign::CENTER, LabelLongMode::CLIP, 30);

	/*called after widget created.*/

	return; //createParts END
}

static STTWidget* pp = NULL;

void loadbg(Frm09* frm) {
	pp = frm->wMChart1;
	STTStyleLine *p = pp->getStyle()->p_line;

	int ss = 0;//stt::constant::Part::Items;
	int opa = p->getOpa(ss);
	int clr1 = p->getColor(ss);
	int width = p->getWidth(ss);
	int dashgap = p->getDashGap(ss);
	int dashwidth = p->getDashWidth(ss);
	int round = p->getRounded(ss)?1:0;

	frm->wMSlider9->setValue(opa, true);

	frm->wMSlider2->setValue((clr1>>16)&0xff, true);
	frm->wMSlider3->setValue((clr1>>8)&0xff, true);
	frm->wMSlider4->setValue((clr1>>0)&0xff, true);

	frm->wMSlider10->setValue(width, true);

	frm->wMSlider11->setValue(dashgap, true);
	frm->wMSlider12->setValue(dashwidth, true);

	frm->wMSlider13->setValue(round, true);

}

void Frm09::onLoad() {
	AsynWorker *worker = new AsynWorker(getRuntime()->getAppInstance(this_thread::get_id()));
	//如需传入局部变量，可在网上搜索lambda表达式的用法
	worker->invoke([this]() {
		std::this_thread::sleep_for(std::chrono::milliseconds(200));//延时200ms
	})->onFinished([this]() {
		loadbg(this);//加载完成时，为确保控件属性已刷新，延时读取控件属性
	})->start();

}

bool Frm09::onClosing() {
	return true;
}

void Frm09::onDispose() {

}

void Frm09::onMyEvent(uint16_t code, LvEvent e) {
	cout<<"onMyEvent"<<endl;
}

static int itmp = 0;

void updatebg(Frm09* frm) {
	pp = frm->wMChart1;
	int ss = stt::constant::Part::Items;
	STTStyleLine *p = pp->getStyle()->p_line;

	int opa = frm->wMSlider9->getValue();
	p->setOpa(opa);
	{
		int r1 = frm->wMSlider2->getValue();
		int g1 = frm->wMSlider3->getValue();
		int b1 = frm->wMSlider4->getValue();
		int rgb1 = (r1<<16)+(g1<<8)+b1;
		p->setColor(rgb1);
	}
	int width = frm->wMSlider10->getValue();
	p->setWidth(width);

	int ofx = frm->wMSlider11->getValue();
	p->setDashGap(ofx);
	int ofy = frm->wMSlider12->getValue();
	p->setDashWidth(ofy);

	int spread = frm->wMSlider13->getValue();
	p->setRounded(spread==1);

	//测试 round
	p->setRounded(spread==1, ss);//曲线是否圆帽
	p->setWidth(8, ss);//曲线线宽度
	frm->wMChart1->setNextValue(frm->wMChart1->getSeries(0), 20);//加一点数据进去
	frm->wMChart1->getStyle()->p_size->setSize(0,stt::constant::Part::Indic);//把散点大小设为0
}

void Frm09::wMSlider2_vch_cb(uint16_t code, LvEvent e) {
	updatebg(this);
}

void Frm09::wMSlider3_vch_cb(uint16_t code, LvEvent e) {
	updatebg(this);
}

void Frm09::wMSlider4_vch_cb(uint16_t code, LvEvent e) {
	updatebg(this);
}

void Frm09::wMSlider9_vch_cb(uint16_t code, LvEvent e) {
	updatebg(this);
}

void Frm09::wMSlider10_vch_cb(uint16_t code, LvEvent e) {
	updatebg(this);
}

void Frm09::wMSlider11_vch_cb(uint16_t code, LvEvent e) {
	updatebg(this);
}

void Frm09::wMSlider12_vch_cb(uint16_t code, LvEvent e) {
	updatebg(this);
}

void Frm09::wMSlider13_vch_cb(uint16_t code, LvEvent e) {
	updatebg(this);
}
